<template>
  <el-dialog
    v-model="dialogVisible"
    :title="$t('team.createTeam')"
    width="600px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="top"
    >
      <el-form-item :label="$t('team.teamName')" prop="name">
        <el-input
          v-model="form.name"
          :placeholder="$t('team.teamNamePlaceholder')"
          maxlength="50"
          show-word-limit
        />
      </el-form-item>

      <el-form-item :label="$t('team.teamDescription')" prop="description">
        <el-input
          v-model="form.description"
          type="textarea"
          :placeholder="$t('team.teamDescriptionPlaceholder')"
          :rows="4"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('team.maxMembers')" prop="maxMembers">
            <el-input-number
              v-model="form.maxMembers"
              :min="2"
              :max="200"
              :step="1"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('team.teamAvatar')" prop="teamAvatarUrl">
            <el-input
              v-model="form.teamAvatarUrl"
              :placeholder="$t('team.teamAvatarPlaceholder')"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 头像预览 -->
      <el-form-item v-if="form.teamAvatarUrl">
        <div class="avatar-preview">
          <el-avatar :size="64" :src="form.teamAvatarUrl" />
          <span class="preview-label">{{ $t('team.avatarPreview') }}</span>
        </div>
      </el-form-item>

      <!-- VIP权限提示 -->
      <el-alert
        :title="$t('team.vipRequired')"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 20px"
      >
        <template #default>
          <p>{{ $t('team.vipRequiredDesc') }}</p>
          <ul>
            <li>{{ $t('team.vipBenefit1') }}</li>
            <li>{{ $t('team.vipBenefit2') }}</li>
            <li>{{ $t('team.vipBenefit3') }}</li>
          </ul>
        </template>
      </el-alert>

      <!-- 审核提示 -->
      <el-alert
        :title="$t('team.approvalRequired')"
        type="warning"
        :closable="false"
        show-icon
      >
        {{ $t('team.approvalRequiredDesc') }}
      </el-alert>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">
          {{ $t('common.cancel') }}
        </el-button>
        <el-button 
          type="primary" 
          @click="handleSubmit"
          :loading="loading"
          :disabled="!canSubmit"
        >
          {{ $t('team.createTeam') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useTeamStore } from '@/stores/teamStore'
import { useAuthStore } from '@/stores/auth'
import type { CreateTeamRequest } from '@/types/team'

interface Props {
  modelValue: boolean
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()
const { t } = useI18n()

const teamStore = useTeamStore()
const authStore = useAuthStore()
const formRef = ref<FormInstance>()
const loading = ref(false)

// 表单数据
const form = reactive<CreateTeamRequest>({
  name: '',
  description: '',
  maxMembers: 50,
  teamAvatarUrl: ''
})

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 是否可以提交
const canSubmit = computed(() => {
  // 直接使用auth store的计算属性
  return authStore.canCreateTeam
})

// 表单验证规则
const rules: FormRules = {
  name: [
    { required: true, message: t('team.teamNameRequired'), trigger: 'blur' },
    { min: 2, max: 50, message: t('team.teamNameLength'), trigger: 'blur' }
  ],
  description: [
    { max: 500, message: t('team.teamDescriptionLength'), trigger: 'blur' }
  ],
  maxMembers: [
    { required: true, message: t('team.maxMembersRequired'), trigger: 'blur' },
    { type: 'number', min: 2, max: 200, message: t('team.maxMembersRange'), trigger: 'blur' }
  ],
  teamAvatarUrl: [
    { type: 'url', message: t('team.teamAvatarInvalid'), trigger: 'blur' }
  ]
}

// 方法
const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    
    if (!canSubmit.value) {
      ElMessage.error(t('team.vipRequired'))
      return
    }

    loading.value = true
    await teamStore.createTeam(form)
    
    emit('success')
    resetForm()
  } catch (error) {
    console.error('创建团队失败:', error)
  } finally {
    loading.value = false
  }
}

const handleClose = () => {
  dialogVisible.value = false
  resetForm()
}

const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  Object.assign(form, {
    name: '',
    description: '',
    maxMembers: 50,
    teamAvatarUrl: ''
  })
}

// 监听对话框打开
watch(dialogVisible, (visible) => {
  if (visible) {
    resetForm()
  }
})
</script>

<style scoped lang="scss">
.avatar-preview {
  display: flex;
  align-items: center;
  gap: 12px;

  .preview-label {
    font-size: 14px;
    color: var(--el-text-color-regular);
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

:deep(.el-alert) {
  .el-alert__content {
    ul {
      margin: 8px 0 0 0;
      padding-left: 20px;

      li {
        margin-bottom: 4px;
        color: var(--el-text-color-regular);
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  :deep(.el-dialog) {
    width: 90% !important;
    margin: 5vh auto;
  }

  .el-row {
    .el-col {
      margin-bottom: 20px;
    }
  }
}
</style>
